package com.serie402.web.action;

import org.apache.log4j.Logger;

import com.kiss.fw.action.AbstractAction;
import com.kiss.fw.context.ActionContext;
import com.kiss.fw.exceptions.ActionException;
import com.kiss.fw.exceptions.ResourceNotFoundException;
import com.kiss.fw.utils.Util;
import com.serie402.common.delegate.EventDelegate;
import com.serie402.common.dto.EventDTO;
import com.serie402.common.dto.page.EventPageDTO;
import com.serie402.common.utils.Constants;

public final class EventAction extends AbstractAction {
	private static final Logger logger = Logger.getLogger(EventAction.class);
	private static EventPageDTO eventsPage;
	
	@Override
	public boolean validate(final ActionContext _context) {
		return true;
	}
	
	public void execute(ActionContext _context) throws ActionException {
		EventDTO dto = null;
		
		try {
			if(_context.isCreateAction()) {
				// TODO: Implement
				dto = new EventDTO();
				EventDelegate.create(dto);
			}
			else if(_context.isReadAction()) {
				dto = new EventDTO(getEventId(_context));
				EventDelegate.read(dto);
			}
			else if(_context.isUpdateAction()) {
				// TODO: Implement
				dto = new EventDTO();
				EventDelegate.update(dto);
			}
			else if(_context.isDeleteAction()) {
				dto = new EventDTO(getEventId(_context));
				EventDelegate.delete(dto);
			}
			else if(_context.isSearchAction()) {
				// TODO: Implement
				dto = new EventDTO();
				EventDelegate.search(dto);
				_context.setRequestAttribute(Constants.Mappings.EVENTS_DTO, eventsPage);
			}
		}
		catch(Exception _exception) {
			handleActionException(logger, "execute", _exception);
		}
	}
	
	private static int getEventId(final ActionContext _context) throws ResourceNotFoundException {
		int eventId = Util.parseInt(_context.getRequest().getParameter("eventId"), -1);
		
		if(eventId == -1) {
			throw com.kiss.fw.utils.Constants.Exceptions.RESOURCE_NOT_FOUND_EXCEPTION;
		}
		
		return eventId;
	}
	
	@Override
	public void create(ActionContext _context) throws ActionException {
	}
	
	@Override
	public void read(ActionContext _context) throws ActionException {
	}
	
	@Override
	public void update(ActionContext _context) throws ActionException {
	}
	
	@Override
	public void delete(ActionContext _context) throws ActionException {
	}
	
	@Override
	public void search(ActionContext _context) throws ActionException {
	}
	
	@Override
	public void browse(ActionContext _context) throws ActionException {
	}
}